﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Diagnostics;
using NewThinkings.Framework.Database;
using System.Configuration;
namespace NewThinkings.DataLayer.DataObjects
{
    public class CDaoBase
    {
        protected static string sConnectionString =ConfigurationSettings.AppSettings["ConnectionString"];
        //protected static string sConnectionString = "Data Source=XAM;Initial Catalog=VnAccounting;MultipleActiveResultSets=True;Max Pool Size=500;User ID=admin;Password=admin;Connection Timeout=900";
        protected SqlConnection conn = null;
        protected SqlCommand command;
        protected SqlDataAdapter adapter;
        private const string CALLFUNCTIONSTORENAME = "core.usp_SYS_VRF_CallFunction";

        public CDaoBase()
        {
            conn = new SqlConnection(sConnectionString);
        }

        /// <summary>
        /// Open Sql Connection
        /// </summary>
        private void OpenConnection()
        {

            if (conn == null) conn = new SqlConnection(sConnectionString);
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }

        }

        /// <summary>
        /// Close Sql Connection
        /// </summary>
        private void CloseConnection()
        {
            if (conn == null) conn = new SqlConnection(sConnectionString);
            if (conn.State != ConnectionState.Closed)
            {
                conn.Close();
            }
        }
        /// <summary>
        /// Excecute store by FunctionID
        /// </summary>
        /// <param name="functionID"></param>
        /// <param name="inputValue"></param>
        /// <returns></returns>
        protected DataSet CallFunction(int functionID, string inputValue)
        {
            try
            {
                command = new SqlCommand(CALLFUNCTIONSTORENAME, conn);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Clear();
                command.Parameters.Add("@InputValue", inputValue);
                command.Parameters.Add("@FunctionID", functionID);

                adapter = new SqlDataAdapter(command);
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                string stack = GetStackTrace(new StackTrace());
                NewThinkings.Framework.Log.CLogManager.WriteDAL("CallFunction", stack + "::::" + ex.Message);
                return null;
            }
        }

        protected IList<T> CallFunctionWithList<T>(int functionID, string inputValue) where T : new()
        {
            DataSet ds = CallFunction(functionID, inputValue);
            //List<T> list = new List<T>();
            if (ds == null) return null;
            if (ds.Tables.Count == 0) return null;
            return CDb.MapList<T>(ds.Tables[0]);

            //return null;
        }

        protected T CallFunction<T>(int functionID, string inputValue) where T : new()
        {
            DataSet ds = CallFunction(functionID, inputValue);
            //List<T> list = new List<T>();
            if (ds == null) return default(T);
            if (ds.Tables.Count == 0) return default(T);
            return CDb.Map<T>(ds.Tables[0]);

            //return null;
        }

        protected string GetStackTrace(StackTrace trace)
        {
            string stack = "";
            for (int i = trace.FrameCount - 1; i >= 0; i--)
            {
                string methodName = trace.GetFrame(i).GetMethod().Name;
                string moduleName = trace.GetFrame(i).GetMethod().ReflectedType.Name;
                stack = stack + "/" + moduleName + "." + methodName;
            }
            return stack;
        }
    }
}
